package saxion.sensorframework.service;

import java.sql.SQLException;
import java.util.Properties;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.apache.log4j.Logger;
import saxion.sensorframework.SensorProcessor;
import saxion.sensorframework.SensorValidator;
import saxion.sensorframework.db.dao.DaoSensor;
import saxion.sensorframework.structuur.Sensor;

/**
 * Webservice voor sensors
 * 
 * @author Erik & Martijn
 * 
 */
@WebService
public class SensorWebService {

	private Properties properties;
	/** < Properties file. \sa SensorFramework::properties */
	static Logger logger = Logger.getLogger(SensorWebService.class.getName());
	/** < logger \sa SensorFramework::logger */

	private String sensorXmlLocation;
	/** < Defined in propertie file. \sa SensorFramework::properties */
	private String dataXmlLocation;
	/** < Defined in propertie file. \sa SensorFramework::properties */

	private int sensorId = 0;
	/** < id for the temp sensor files created by SensorWebService */
	private int sensorDataId = 0;
	/** < id for the temp sensordata files created by SensorWebService */

	private SensorValidator sensorValidator;
	/** < used to validate sensor related data */
	private SensorProcessor sensorProcessor;
	/** < used to help with sensor related tasks */
	private DaoSensor daoSensor;

	/** < DaoSensor used for database connection */

	/**
	 * SensorWebservice
	 * 
	 * @param properties
	 *            \sa SensorFramework::properties
	 */
	public SensorWebService(Properties properties) {
		this.properties = properties;
		initialize();
	}

	/**
	 * \brief initialize variables.
	 * 
	 * Set sensor and data location, create daoSensor, Create sensor validator
	 * and sensorprocessor.
	 * 
	 * \sa daoSensor \sa sensorValidator \sa sensorProcessor
	 * 
	 */
	private void initialize() {
		sensorXmlLocation = properties.getProperty("sensorXmlLocation");
		dataXmlLocation = properties.getProperty("dataXmlLocation");

		try {
			sensorValidator = new SensorValidator(properties);
		} catch (Exception e) {
			logger.error("Unable to create sensorValidator");
			e.printStackTrace();
			System.exit(-1);
		}

		try {
			sensorProcessor = new SensorProcessor();
		} catch (Exception e) {
			logger.error("Unable to create sensorProcessor");
			e.printStackTrace();
			System.exit(-1);
		}

		try {
			daoSensor = new DaoSensor();
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Unable to create DaoSensor");
			System.exit(-1);
		}
	}

	/**
	 * \brief Register Sensor.
	 * 
	 * Use xml file to register new sensor. Used \link sensorValidator \endlink,
	 * \link sensorProcessor \endlink and \link daoSensor \endlink
	 * 
	 * @param xml
	 *            containing the sensor information
	 * @return sensor id, generated by framework
	 */
	@WebMethod
	public int registerSensor(byte[] xml) {
		logger.info("Registering new Sensor..");
		String xmlLocation = sensorXmlLocation + "/Sensor" + sensorId + ".xml";

		sensorProcessor.saveDataToFile(xml, xmlLocation);

		sensorId++;

		if (!sensorValidator.validateSensorXML(xmlLocation)) {
			logger.error("Unable to validate sensorxml");
			return -1;
		}

		Sensor sensor = sensorProcessor.convertXMLtoObject(xmlLocation);

		if (sensor == null) {
			logger.error("unable to create Sensor Object from xml!");
			return -1;
		}

		int id = -1;
		try {
			id = daoSensor.addSensorAndDataTable(sensor);
		} catch (SQLException e) {
			logger.error("Unable to add new Sensor");
			logger.error(e.toString());
			e.printStackTrace();
			return -1;
		}

		logger.info("New Sensor! {" + sensor.toString() + "}");

		return id;
	}

	/**
	 * \brief register sensordata
	 * 
	 * Used \link sensorProcessor \endlink to process data to database.
	 * 
	 * @param xml
	 *            containing the sensor data
	 * @return true if no error occurred false if error occurred
	 */
	@WebMethod
	public boolean sendSensorData(byte[] xml) {
		logger.info("New Sensor data!");
		String xmlLocation = dataXmlLocation + "/dataSensor" + sensorDataId
				+ ".xml";
		sensorProcessor.saveDataToFile(xml, xmlLocation);
		sensorDataId++;

		try {
			sensorProcessor.readXMLtoDatabase(xmlLocation);
		} catch (Exception e) {
			logger.error("Error saving sensor data to database");
			e.printStackTrace();
			return false;
		}

		return true;
	}

}
